/**
 * 
 */
package com.alphasystem.commerce.common.model.folder;

import static javax.persistence.FetchType.LAZY;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.alphasystem.commerce.common.model.AbstractEntity;

/**
 * @author sali
 * 
 */
@Entity
@Table(name = "FOLDER")
@Inheritance
@DiscriminatorColumn(name = "FOLDER_TYPE")
public class Folder extends AbstractEntity {

	private static final long serialVersionUID = 4221630930202951675L;

	protected String name;

	protected Folder parentFolder;

	protected Set<Folder> childFolders;

	public Folder() {
		super();
	}

	public Folder(String id) {
		super(id);
	}

	@Transient
	public String getAbsolutePath() {
		String path = "/" + name;
		if (parentFolder != null) {
			path = parentFolder.getAbsolutePath() + path;
		}
		return path;
	}

	@OneToMany(mappedBy = "parentFolder", fetch = LAZY)
	public Set<Folder> getChildFolders() {
		return childFolders;
	}

	@Column(name = "NAME", length = 255, nullable = false)
	public String getName() {
		return name;
	}

	@ManyToOne
	@JoinColumn(name = "PARENT_FOLDER_ID")
	public Folder getParentFolder() {
		return parentFolder;
	}

	public void setChildFolders(Set<Folder> childFolders) {
		this.childFolders = childFolders;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setParentFolder(Folder parentFolder) {
		this.parentFolder = parentFolder;
	}

}
